package engine.gui.elements;

import static org.lwjgl.opengl.GL11.glColor4f;
import engine.gui.Container;
import engine.gui.GUIStyle;
import engine.util.ortho.Draw2D;
import engine.util.vector.Color;

public class ScrollPane extends Container{
	public static final int ALIGN_NONE = 0;
	public static final int ALIGN_TOP = 1;
	public static final int ALIGN_LEFT = 1;
	public static final int ALIGN_BOTTOM = 2;
	public static final int ALIGN_RIGHT = 2;
	public static final int ALIGN_CENTER = 3;
	
	private int align; 			//horizontal alignment of the panel
	private int valign;			//vertical alignment of the panel 
	
	//variables, to store, where the panel has been painted
	private int drawX;			
	private int drawY;
	
	//variables, to store the scrolling of the scrollpane
	private int scrollX;
	private int scrollY;
	
	//variables, which store the maximum bounds
	private int maxX;
	private int maxY;
	
	public ScrollPane() {

	}
	
	public ScrollPane(int posX, int posY, int width, int height) {
		this();
		setPosX(posX);
		setPosY(posY);
		setWidth(width);
		setHeight(height);
	}
	
	@Override
	public void render(int x, int y, int width, int height) {
		switch(align) {
		case ALIGN_LEFT: drawX = getPosX(); break;
		case ALIGN_CENTER: drawX = (width - getWidth()) / 2; break;
		case ALIGN_RIGHT: drawX = width - getWidth() - getPosX(); break;
		default: drawX = getPosX(); break;
		}
		
		switch(valign) {
		case ALIGN_TOP: drawY = getPosY(); break;
		case ALIGN_CENTER: drawY = (height - getHeight()) / 2; break;
		case ALIGN_BOTTOM: drawY = height - getHeight() - getPosY(); break;
		default: drawY = getPosY(); break;
		}
		
		if(isSolid()) {
			GUIStyle style = getCurrentStyle();
			
			Color.bindColor4f(style.getScrollPaneBackgroundColor());
			
			if(style.getScrollPaneBackgroundImage() != null) {
				style.getScrollPaneBackgroundImage().draw(x + drawX, y + drawY, getWidth(), getHeight());
			} else {
				Draw2D.fillRect(x + drawX, y + drawY, getWidth(), getHeight());
			}
			
			if(style.isScrollPaneBorder()) {
				glColor4f(0, 0, 0, 1);
				Draw2D.drawRect(x + drawX, y + drawY, getWidth(), getHeight());
			}
		}
		
		super.render(x + drawX - getPosX(), y + drawY - getPosY(), width, height);
	}
	
	public int getAlign() {
		return align;
	}
	
	public void setAlign(int align) {
		this.align = align;
	}
	
	public void setValign(int valign) {
		this.valign = valign;
	}
	
	public int getValign() {
		return valign;
	}
	
	@Override
	public boolean mousePressed(int posX, int posY, int button) {
		return super.mousePressed(posX + getPosX() - drawX, posY + getPosY() - drawY, button);
	}
	
	@Override
	public boolean mouseReleased(int posX, int posY, int button) {
		return super.mouseReleased(posX + getPosX() - drawX, posY + getPosY() - drawY, button);
	}
	
	@Override
	public boolean mouseMoved(int posX, int posY, int button) {
		return super.mouseMoved(posX + getPosX() - drawX, posY + getPosY() - drawY, button);
	}
	
	@Override
	public boolean mouseDragged(int posX, int posY, int button) {
		return super.mouseDragged(posX + getPosX() - drawX, posY + getPosY() - drawY, button);
	}
}
